// 给定一个整数数组 nums和一个目标值 target，请你在该数组中找出和为目标值的那两个整数，并返回他们的数组下标。
// 你可以假设每种输入只会对应一个答案。但是，数组中同一个元素不能使用两遍。
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
let twoSum1 = function (nums, target) {
    for (let i = 0; i < nums.length; i++) {
        let a = target - nums[i]
        let index = nums.slice(i + 1).indexOf(a)
        if (index !== -1) {
            return [i, index + i + 1]
        }
    }
}
let nums = [3, 3]
let target = 6
let res = twoSum1(nums, target)
console.log(res)

let twoSum2 = function (nums, target) {
    let targetMap = {}
    for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
            targetMap[nums[i] + nums[j]] = [i, j]
        }
    }
    return targetMap[target]
}
let nums = [2, 7, 11, 15]
let target = 9
let res = twoSum2(nums, target)
console.log(res)

let twoSum3 = function (nums, target) {
    let targetMap = {}
    for (let i = 0; i < nums.length; i++) {
        targetMap[nums[i]] = i
    }
    for (let i = 0; i < nums.length; i++) {
        let a = target - nums[i]
        if (targetMap[a] && targetMap[a] !== i) {
            return [i, targetMap[a]]
        }
    }
}
let nums = [0, 3, 3, 2]
let target = 6
// let nums = [2, 7, 11, 15]
// let target = 9
let res = twoSum3(nums, target)
console.log(res)

let twoSum4 = function (nums, target) {
    let index = []
    let map = new Map()
    for (let i = 0; i < nums.length; i++) {
        if (map.has(nums[i])) {
            index[0] = map.get(nums[i])
            index[1] = i
            return index
        }
        map.set(target - nums[i], i)
    }
    return index
}
// let nums = [0, 3, 3, 2]
// let target = 6
let nums = [2, 7, 11, 15]
let target = 9
let res = twoSum4(nums, target)
console.log(res)

